package yxy.game.pm2.active.command.celebration;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.acttreasuredata;
import yxy.cherry.data.bean.acttreasurefloprandom;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_国庆寻宝;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopCard;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.NationalDayMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

/**
 * 国庆寻宝抽奖
 *
 * @ClassName CMD_Active_国庆寻宝_NationalDayLottery
 * @Author jingtao-zheng
 * @Date 2022/9/21 20:11
 * @Version 1.0
 **/
@MessageType(proto = Command.NationalDayLottery, classtype = NationalDayMessage.NationalDayLotteryRequest.class)
public class CMD_Active_国庆寻宝_NationalDayLottery extends Command<NationalDayMessage.NationalDayLotteryRequest> {

    @Override
    public void execute(MessageSource messageSource, NationalDayMessage.NationalDayLotteryRequest request) throws Exception {
        String playercode = messageSource.getTag();
        NationalDayMessage.NationalDayLotteryResponse.Builder response = NationalDayMessage.NationalDayLotteryResponse.newBuilder();
        int pos = request.getPos();//位置
        try {
            ActiveModule_国庆寻宝 am = ActiveModule_国庆寻宝.get(playercode, false);
            if (am.tryLock()) {
                try {
                    FlopCard flopCard = am.getFcmap().get(pos);
                    //已翻开的格子且不是终极大奖再翻就直接报错
                    if(flopCard.isFlop()){
                        send(messageSource, response, Result.DataError);
                        return;
                    }
                    int id = flopCard.getId();
                    acttreasurefloprandom random = DataCenter.getData(id, acttreasurefloprandom.class);
                    Currency currency = Currency.create(random.getRewardtype(), random.getRewardid(), random.getRewardnum());
                    NationalDayMessage.NationalDayLotteryCell.Builder cell = NationalDayMessage.NationalDayLotteryCell.newBuilder();
                    cell.setPos(pos);
                    if(currency!=null){
                        PackageMessage.Item item = new CurrencyHandle().packItem(currency);
                        response.setReward(item);
                        cell.setReward(item);
                    }
                    if (random.getType()== Constant.ONE) {
                        cell.setNext(false);
                    }else if(random.getType()== Constant.TWO){
                        cell.setNext(true);
                        if(!flopCard.isFlop()){
                            am.setFloor(am.getFloor()+1);
                        }
                    }else{
                        send(messageSource, response, Result.DataError);
                        return;
                    }
                    response.setCell(cell);
                    //扣除抽奖道具
                    acttreasuredata data = DataCenter.getData(1, acttreasuredata.class);
                    Currency costcur = Currency.create(CurrencyType.CT_物品, data.getCostid(), 1);
                    boolean canbuy = new CurrencyHandle().costCheck(playercode, costcur);
                    if (!canbuy) {
                        send(messageSource, response, new Result(Result.CostFail, CurrencyType.CT_物品.getNumber(), data.getCostid()));
                        return;
                    }
                    new CurrencyHandle().cost(playercode, CCReason.活动_翻牌, costcur);
                    //寻宝普通奖励发放-条件是不是下一层,且之前没翻开过
                    if(currency!=null&&(!flopCard.isFlop())){
                        new CurrencyHandle().addCurrency(playercode, CCReason.国庆寻宝, currency);
                    }
                    flopCard.setFlop(true);
                    am.set();
                    send(messageSource, response, Result.Success);
                } catch (Exception e) {
                    e.printStackTrace();
                    send(messageSource, response, Result.DataError);
                } finally {
                    am.unlock();
                }
            }else{
                send(messageSource, response, Result.DataError);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, NationalDayMessage.NationalDayLotteryResponse.Builder response, Result
            result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}


